package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.time.LocalDate;

public class CalendarWithTextArea {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("日历 - 正方形网格");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);

            JPanel datePanel = new JPanel(new GridLayout(7, 7)); // 7行7列，显示7天
            Font font = new Font("宋体", Font.PLAIN, 14);

            // 添加星期标题
            String[] daysOfWeek = {"一", "二", "三", "四", "五", "六", "日"};
            for (String day : daysOfWeek) {
                JLabel label = new JLabel(day, SwingConstants.CENTER);
                label.setFont(font);
                label.setForeground(Color.DARK_GRAY);
                datePanel.add(label);
            }

            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            LocalDate firstDayOfMonth = currentDate.withDayOfMonth(1);
            int startDayOfWeek = firstDayOfMonth.getDayOfWeek().getValue(); // 获取1号是星期几
            int daysInMonth = currentDate.lengthOfMonth();

            // 填充空白格
            for (int i = 1; i < startDayOfWeek; i++) {
                datePanel.add(new JLabel()); // 空白占位
            }

            // 填充日期格
            for (int day = 1; day <= daysInMonth; day++) {
                String dateKey = currentDate.getYear() + "-" + currentDate.getMonthValue() + "-" + day;

                // 创建一个新的 JPanel 包含日期标签和文本框
                JPanel dayPanel = new JPanel();
                dayPanel.setLayout(new BorderLayout());
                dayPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); // 设置正方形边框

                // 创建日期标签
                JLabel dateLabel = new JLabel(String.valueOf(day), SwingConstants.CENTER);
                dateLabel.setFont(font);
                dateLabel.setForeground(Color.BLACK);
                dayPanel.add(dateLabel, BorderLayout.NORTH);

                // 创建文本框来输入备注
                JTextArea dateTextArea = new JTextArea();
                dateTextArea.setFont(font);
                dateTextArea.setLineWrap(true);
                dateTextArea.setWrapStyleWord(true);
                dateTextArea.setOpaque(true);
                dateTextArea.setBackground(Color.WHITE);
                dateTextArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

                // 如果是今天的日期，设置蓝色背景
                if (currentDate.getDayOfMonth() == day) {
                    dateLabel.setBackground(new Color(70, 130, 180)); // 设置背景颜色
                    dateLabel.setForeground(Color.WHITE); // 设置文字颜色为白色
                    dateLabel.setOpaque(true);
                }

                dayPanel.add(dateTextArea, BorderLayout.CENTER);

                // 当焦点丢失时保存文本
                dateTextArea.addFocusListener(new FocusAdapter() {
                    @Override
                    public void focusLost(FocusEvent e) {
                        // 这里可以保存数据，例如保存到一个 JSON 文件
                        System.out.println("Text for " + dateKey + ": " + dateTextArea.getText());
                    }
                });

                // 将包含日期和文本的 JPanel 添加到主面板
                datePanel.add(dayPanel);
            }

            frame.add(datePanel);
            frame.setVisible(true);
        });
    }
}
